/**
 * 采购管理工具类，提供采购管理各模块公用方法(功能)
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","bsMsgServ", "init","btnServ","bsButton","formServ","codeDef",'uploadfiles','bsUtil',
	"buttonEvent",'moment','bootstrap-datetimepicker','bootstrap-datetimepicker.zh-CN','dateUtil','flowSelect','oaMain'],
		function($,datagridServ, bsMsgServ, init, btnServ,bsButton,formServ,codeDef,uploadfiles,bsUtil,
			buttonEvent,moment,datetimepicker,datetimepickerLang,dateUtil,flowSelect,oaMain) {
	
	'use strict';
	
	var public_method = {			 
			
			/**
			 * @param _options.archiveCode	附件类型代码
			 * @param _options.rootid		根节点id
			 * @param _options.archiveList	附件清单说明
			 * @param _options.name			计划名称
			 * @param _options.id			计划id
			 */
			loadArchiveForm:function(_options){
				//业务表单路径
				var _bizPageUrl = '/'+ init.getSite()+ '/procurementDirect/query/getPage.action?code=bsArchiveForm';
				
				var _c = $('#'+_options.rootid + ' .right-container');
				if(_c.length==1){
					_c.empty();
					_c.load(_bizPageUrl,function(){
						//判断计划是否已保存
						if(_options && _options.data.id){
							
							//显示计划名称
							if(_options.data.name){									
								$('#'+_options.rootid + ' #name').val(_options.data.name)	
							}
							
							//显示附件清单说明
							var _listHtml = '';
							if(_options.archiveList){									
								$.each(_options.archiveList,function(o,j){
									_listHtml +='<p>'+j+'</p>';								
								})									
							}
							var _listContainer = $('#'+_options.rootid + ' .right-container .user-content');
							if(_listContainer.length==1){
								_listContainer.empty();
								_listContainer.append(_listHtml);
							}
							
							//加载附件上传下载表单
							//初始化附件预览
							var _param = {
				    			bizcode 	: _options.archiveCode,
				    			rootid  	: _options.rootid,
				    			containerid : 'archive-container',
				    			bizno		: _options.data.id,
				    			del			: true
				    		}		    		
							bsUtil.initUpload(_param)	
							
						}
					});									
				}
				
			},
			/**
			 * @Param _options.flowid	流程记录唯一编码
			 * @param _options.flowBiz	流程业务代码
			 * @Param _options.rootid	dom 根节点
			 */
			loadFlowForm:function(_options){
				
				var _flowid,_flowBiz,_code , _rootid
				
				if(_options){
					_flowid = _options.flowid;
					_flowBiz = _options.flowBiz;
					_rootid = _options.rootid;
				}
				
				if(_flowBiz){
					//业务表单路径
					var _bizPageUrl = '/'+ init.getSite()+ '/common/query/getBizPage.action?subSystem=oa&code=bsEditDlg&module='+_flowBiz;
					
					var _c = $('#' + _rootid + ' .right-container')
					//判断业务记录是否存在
					if(_flowid){
						
						//业务记录存在，查询业务记录数据
						var _urlBase = '/' + init.getSite() +'/' + _flowBiz;						
						//获取流程记录						
						var bizRowUrl = _urlBase +'/query/getOneByBizTable.action';
						$.ajax({
							url:bizRowUrl,
							type:'post',
							contentType:'application/x-www-form-urlencoded; charset=UTF-8',
							data:{field:'flowInstId',fieldVal:_flowid},
							success:function(data){
								//清除原有页面
								
								if(_c && _c.length===1){
									_c.empty();	
									
									//加载审批流程表单页面
									
									_c.load(_bizPageUrl,function(){
										//加载流程表单数据
										var _form = $('#' + _rootid + ' .main-form')
										_form.form('load',data);
									});																		
								}								
							},
							error:function(){
								bsMsgServ.errorSlide('获取流程信息失败，请联系系统管理员进行处理！');
							}
						})
					}else{
						//业务记录不存在，只加载页面
						_c.load(_bizPageUrl);
					}
				}
			}
			,
			
			/**
			 * @Param _options.flowid	流程记录唯一编码
			 * @param _options.flowBiz	流程业务代码
			 * @Param _options.rootid	dom 根节点
			 */
			loadFlowViewForm:function(_options){
				
				var _flowid,_flowBiz,_code , _rootid
				
				if(_options){
					_flowid = _options.flowid;
					_flowBiz = _options.flowBiz;
					_rootid = _options.rootid;
				}
				
				if(_flowBiz){
					//业务表单路径
					var _bizPageUrl = '/'+ init.getSite()+ '/common/query/getBizPage.action?subSystem=oa&code=bsEditDlg&module='+_flowBiz;
					
					var _c = $('#' + _rootid + ' .right-container')					
						
					//业务记录存在，查询业务记录数据
					var _urlBase = '/' + init.getSite() +'/' + _flowBiz;					
					
							
					if(_c && _c.length===1){
						
						_c.load(_bizPageUrl);
					}
				}
			}
			,
			/**
			 * 空表单提示信息
			 * @Param _options.rootid	
			 * 
			 */
			loadEmptyForm:function(_options){
				
				if(_options && _options.rootid){
					var _rootid = _options.rootid;
					var _c = $('#' + _rootid + ' .right-container')
					if(_c && _c.length===1){
						_c.empty();	
						_c.html('<div><p>未找到关联业务记录，请点击新增按钮添加相应记录!!!</p></div>')
					}
				}
			}
			
			,
			/**
			 * 流程操作成功提示信息
			 * @Param _options.rootid	
			 * 
			 */
			loadSuccessForm:function(_options){
				
				if(_options && _options.rootid){
					var _rootid = _options.rootid;
					var _c = $('#' + _rootid + ' .right-container')
					if(_c && _c.length===1){
						_c.empty();	
						_c.html('<div><p>流程操作已成功，请点击 <b>采购计划</b>刷新记录!!!</p></div>')
					}
				}
			}
			,
			/**
			 * 加载流程业务模块按钮
			 * @Param _options.rootid 	dom根节点id
			 * @Param _options.flowBizcode	流程业务代码
			 */
			loadBtn:function(_options){
				var _df = {
					bizcode	: _options.flowBizcode,
					bizType	: 1,
					btnType	: 'edit'
				}
				var _btn = bsButton.getBtn(_df);	
				
				var _c = $('#' + _options.rootid + ' .btn-container')
				if(_c){
					_c.empty();
					
					if(_btn){
						_c.append(_btn);
					}
				}
			}
			
			,
			/**
			 * 加载流程业务模块查看页面按钮
			 * @Param _options.rootid 	dom根节点id
			 * @Param _options.flowBizcode	流程业务代码
			 */
			loadViewBtn:function(_options){
				var _df = {
					bizcode	: _options.flowBizcode,
					bizType	: 1,
					btnType	: 'view'
				}
				var _btn = bsButton.getBtn(_df);	
				
				var _c = $('#' + _options.rootid + ' .btn-container')
				if(_c){
					_c.empty();
					
					if(_btn){
						_c.append(_btn);
					}
				}
			}
			,
			
			/**
			 * 加载新增按钮
			 */
			loadAdd:function(_options){
				var btn = '<button type="button" class="btn btn-warning btn-sm pull-right" btn-type="listAdd">'+
				'<span class="glyphicon glyphicon-plus"></span> 新增'+	           
			'</button>'
			}
			
			,
			/**
			 * 验证用户是否签收流转业务
			 * @Param _options.flowInstId	流转业务实例id
			 * @Param _options.userid		用户id
			 */
			validateFlowUser:function(_options){
				
				var vUrl = '/oa/flowOperator/validateFlowUser.action'
					
				var ret ;
				if(_options && _options.flowInstId && _options.userid){
					$.ajax({
						url: vUrl,
						async: false,
						contentType:'application/x-www-form-urlencoded; charset=UTF-8',
						data:{'flowInstId':_options.flowInstId,userid:_options.userid},
						success:function(data){
							if(data){
								ret = data
							}
							
														
						},
						error:function(){
							bsMsgServ.errorSlide('获取流程信息失败，请联系系统管理员进行处理！');
						}
					})
				}
				return ret;
			}
			
			,
			/**
			 * 获取返回的流程类型
			 * @param _options.data		返回的业务记录
			 * @param _options.moduCode	定义好的业务模块代码
			 */
			getBackFlowInfo:function(_options){
				
				var _data = {},data,moduCode
				
				if(_options){
					data = _options.data;
					moduCode = _options.moduCode;
				}
				if(data && data.bizcode && data.flowInstId){
											
					switch(data.bizcode){
						case moduCode.planSchema:
							_data = {
								schema_no:data.flowInstId,							
								schema_bizcode:data.bizcode,
								status 			: 4,
								status_name 	: '工作方案审批'
							}
							//工作方案
							break;
						case moduCode.bidPublic:
							_data = {
								bid_no:data.flowInstId,							
								bid_bizcode:data.bizcode,
								status 			: 6,
								status_name 	: '招标公告'
							}
							//招标公告
							break;
						case moduCode.bargain:
							_data = {
								bargain_no:data.flowInstId,							
								bargain_bizcode:data.bizcode,
								status 			: 6,
								status_name 	: '议价结论审批'
							}
							//议价结论
							break;
							
						case moduCode.contract:
							_data = {
								contract_no:data.flowInstId,							
								contract_bizcode:data.bizcode,
								status 			: 8,
								status_name 	: '合同审批'
							}
							//合同
							break;
							
						case moduCode.applyBizcode	:
							//审批申请
							_data = {
								approval_no:data.flowInstId,							
								approval_bizcode:data.bizcode,
								status 			: 2,
								status_name 	: '计划审批'
							}
							
							break;
							
						default:									
					}					
				}
				
				//完成采购标志
				if(data.complete_status && data.complete_status=='1'){
					_data.status = 10;
					_data.status_name='采购完成';
				}
				return _data;
			}
			
			,
			statusShow:function(value,row,index){
				
				if(window.console){
					console.log('status show running...')
				}
				var _value = 1,_name ='计划填报'
				
				if(value){								
					_value = parseInt(value) * 10
				}
				
				if(row.status_name){
					_name = row.status_name;
				}
				return '<div class="progress progress-striped active" style="margin-bottom:0px;">'+
				    '<div class="progress-bar progress-bar-success" role="progressbar"'+
				         'aria-valuenow="60" aria-valuemin="0" aria-valuemax="100"'+
				         'style="width: '+_value+'%;color:#d53388">'+_name+'</div>'+
				'</div>';
											
			},
			
			getGridParam:function(){
				var _detailParam = {				
				
					height:init.getGridHeight(),				
					rownumbers:true,
					animate: true,									
					striped:true,
					border:true,
					//singleSelect:false,
					idField: 'id',									
					fitColumns: true,
					checkOnSelect:true,
					pagination : true,
					selectOnCheck:true,
					columns:[[
						{field:'ck',checkbox:true},					     
						{field:'name',title:'项目名称',width:100,halign:'center',
							formatter:function(value,row,index){
								if(value){
									return '<span class=/"easyui-tooltip/" title="'+value +'">'+value +'</span>';
								}
								return '';						
							}
						},        
						
						{field:'apply_deptname',title:'申请部门',width:50,halign:'center'} ,
						{field:'apply_name',title:'申请人',width:50,halign:'center'},
						{field:'apply_date',title:'申请时间',width:50,halign:'center'} ,						
						{field:'status',title:'当前状态',width:50,align:'center',						
							formatter:public_method.statusShow						
						},
						{field:'complete_status',title:'采购结束',width:20,halign:'center',
							formatter:function(value,row,index){
								if(value && value =='1'){
									return '是'									
								}else{
									return '否'
								}
							}
						} 	
						
					]]
				}
				return _detailParam;
			}
			,
			getCurrentUser:function(){
				var ret = {}
				var usr = init.getCurrentUser();
				if(usr && usr.id){
					ret = {
						apply_id: usr.id,
						apply_name: usr.name,
						apply_deptid: usr.deptid,
						apply_deptname: usr.deptName,
						apply_date: dateUtil.getDateValue({format:'yyyy-MM-dd hh:mi:ss'})
					}
				}
				return ret;
			}
	
	}
	
	return public_method;
})